package com.chinairi.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinairi.common.annotation.DoPage;
import com.chinairi.mall.config.ValidationResult;
import com.chinairi.mall.config.ValidatorImpl;
import com.chinairi.mall.controller.viewobject.TCVO;
import com.chinairi.mall.dao.TrackAlarmConfigMapper;
import com.chinairi.mall.dao.TrackInforConfigMapper;
import com.chinairi.mall.dao.TrackInformationConfigMapper;
import com.chinairi.mall.error.BusinessException;
import com.chinairi.mall.error.EmBusinessError;
import com.chinairi.mall.model.TrackAlarmConfig;
import com.chinairi.mall.model.TrackConfig;
import com.chinairi.mall.dao.TrackConfigMapper;
import com.chinairi.mall.model.TrackInforConfig;
import com.chinairi.mall.model.TrackInformationConfig;
import com.chinairi.mall.service.ITrackConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 节点带物料类别配置表 服务实现类
 * </p>
 *
 * @author Webgasd
 * @since 2020-08-17
 */
@Service
public class TrackConfigServiceImpl extends ServiceImpl<TrackConfigMapper, TrackConfig> implements ITrackConfigService {
    @Autowired
    private TrackConfigMapper trackConfigMapper;
    @Autowired
    private TrackAlarmConfigMapper trackAlarmConfigMapper;
    @Autowired
    private TrackInformationConfigMapper trackInformationConfigMapper;
    @Autowired
    private TrackInforConfigMapper trackInforConfigMapper;
    @Autowired
    private ValidatorImpl validator;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void addPoint(TCVO tcvo) {
        ValidationResult result = validator.validate(tcvo);
        if (result.isHashErrors()){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,result.getErrMsg());
        }

        if(checkAllExist(tcvo.getTrackType(), tcvo.getSeq(), tcvo.getTrackName(), tcvo.getTrackNumber())) {
            throw new BusinessException(EmBusinessError.ITEMS_Exist);
        }

        TrackConfig trackConfig = new TrackConfig();
        BeanUtils.copyProperties(tcvo,trackConfig);
        trackConfig.setTrackNumber(UUID.randomUUID().toString().replace("-",""));
        trackConfigMapper.insert(trackConfig);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void updatePoint(TCVO tcvo) {

        ValidationResult result = validator.validate(tcvo);
        if (result.isHashErrors()){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,result.getErrMsg());
        }

        if(checkAllExist(tcvo.getTrackType(), tcvo.getSeq(), tcvo.getTrackName(), tcvo.getTrackNumber())) {
            throw new BusinessException(EmBusinessError.ITEMS_Exist);
        }

        TrackConfig trackConfig = new TrackConfig();
        BeanUtils.copyProperties(tcvo,trackConfig);
        trackConfigMapper.updateById(trackConfig);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deletePoint(String uuid) {
        trackConfigMapper.deleteById(uuid);

        QueryWrapper<TrackAlarmConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("TRACK_NUMBER",uuid);
        trackAlarmConfigMapper.delete(wrapper);

        QueryWrapper<TrackInformationConfig> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("TRACK_NUMBER",uuid);
        trackInformationConfigMapper.delete(wrapper1);

        QueryWrapper<TrackInforConfig> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("TRACK_NUMBER",uuid);
        trackInforConfigMapper.delete(wrapper2);
    }

    @Override
    @DoPage
    public List<TrackConfig> getPoints(TCVO tcvo) {
        QueryWrapper<TrackConfig> wrapper = new QueryWrapper<>();
        wrapper
                .like(tcvo.getTrackType() != null,"TRACK_TYPE",tcvo.getTrackType())
                .like(StringUtils.isNotEmpty(tcvo.getTrackCategoryInt()),"TRACK_CATEGORY_INT",tcvo.getTrackCategoryInt())
                .orderByAsc("TRACK_TYPE")
                .orderByAsc("SEQ");
        List<TrackConfig> list = trackConfigMapper.selectList(wrapper);
        return list;
    }

    public boolean checkAllExist(String type, Integer seq, String name, String number) {
        return trackConfigMapper.countByAll(type,seq,name,number) > 0;
    }
}
